package cn.gdcp.demo10;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import java.util.ArrayList;

public class StudentAdapter extends BaseAdapter{
    private Context context;
    private ArrayList<Student> studentArrayList;
    private IOnDataDelListener listener;

    public StudentAdapter(Context context, ArrayList<Student> studentArrayList, IOnDataDelListener listener) {
        this.context = context;
        this.studentArrayList = studentArrayList;
        this.listener = listener;
    }

    @Override
    public int getCount() {
        return studentArrayList.size();
    }

    @Override
    public Object getItem(int position) {
        return studentArrayList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View view, ViewGroup parent) {
        if (view == null){
            view = LayoutInflater.from(context).inflate(R.layout.list_item,parent,false);

            StudentViewHolder holder = new StudentViewHolder();
            holder.txtNo = view.findViewById(R.id.txt_no);
            holder.txtName = view.findViewById(R.id.txt_name);
            holder.txtAge = view.findViewById(R.id.txt_age);
            holder.btnDel = view.findViewById(R.id.btn_del);

            view.setTag(holder);
        }


        final Student s = studentArrayList.get(position);
        StudentViewHolder holder = (StudentViewHolder) view.getTag();

        holder.txtNo.setText(s.getStuno());
        holder.txtName.setText(s.getName());
        holder.txtAge.setText(String.valueOf(s.getAge()));

        holder.btnDel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.del(s);
            }
        });

        return view;
    }

}
